package org.atguigu.gmall.realtime.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.atguigu.gmall.realtime.common.Constant;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Project:gmallRealTime
 * Package:org.atguigu.gmall.realtime.util
 * Author: ZengHaiFeng
 * CreateTime:2023/5/5 19:00:07
 * Description: Todo
 * Version：1.0
 */
public class DruidDSUtil {
    private static final DruidDataSource druidDataSource;
    static {
        System.out.println(" ================= get Druid connection pool object ====================");
        druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(Constant.PHOENIX_DRIVER);
        druidDataSource.setUrl(Constant.PHOENIX_URL);
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(100);
        druidDataSource.setMinIdle(2);
        druidDataSource.setMaxWait(-1);
        druidDataSource.setValidationQuery("select 1");
        druidDataSource.setTestWhileIdle(true);
        druidDataSource.setTestOnBorrow(true);
        druidDataSource.setTestOnReturn(false);
        druidDataSource.setTimeBetweenEvictionRunsMillis(30 * 1000L);
        druidDataSource.setMinEvictableIdleTimeMillis(30 * 60 * 1000L);
    }
    public static Connection getConnection() {
        DruidPooledConnection conn;
        try {
            conn = druidDataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn ;
    }
}
